package com.gjr.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gjr.eduservice.entity.EduChapter;
import com.gjr.eduservice.entity.EduVideo;
import com.gjr.eduservice.entity.chapter.ChapterVo;
import com.gjr.eduservice.entity.chapter.VideoVo;
import com.gjr.eduservice.mapper.EduChapterMapper;
import com.gjr.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gjr.eduservice.service.EduVideoService;
import com.gjr.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author gjr
 * @since 2022-09-01
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    @Autowired
    private EduVideoService eduVideoService;

    /**
     * 课程大纲列表，根据课程id进行查询
     *
     * @param courseId 课程id
     * @return 返回章节和小节
     */
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        // 1 根据课程id查询课程里面所有的章节
        List<EduChapter> chapterList = this.list(new LambdaQueryWrapper<EduChapter>()
                .eq(StringUtils.hasText(courseId), EduChapter::getCourseId, courseId));

        // 2 根据课程id查询课程里面所有的小节
        List<EduVideo> videoList = eduVideoService.list(new LambdaQueryWrapper<EduVideo>()
                .eq(StringUtils.hasText(courseId), EduVideo::getCourseId, courseId));

        // 判断如果没有章节直接返回空
        if (chapterList.isEmpty() || chapterList.size() == 0) return null;

        // 3 遍历查询章节list集合进行封装
        // 存放章节和小节
        List<ChapterVo> chapterVoList = new ArrayList<>();
        for (EduChapter eduChapter : chapterList) {
            ChapterVo chapterVo = new ChapterVo();
            chapterVo.setId(eduChapter.getId());
            chapterVo.setTitle(eduChapter.getTitle());

            // 4 遍历查询小节List集合，进行封装
            // 存放小节
            List<VideoVo> videoVoList = new ArrayList<>();
            for (EduVideo eduVideo : videoList) {
                String chapterId = eduVideo.getChapterId();
                boolean videoFlag = eduChapter.getId().equals(chapterId);
                if (videoFlag) {
                    VideoVo videoVo = new VideoVo();
                    videoVo.setId(eduVideo.getId());
                    videoVo.setTitle(eduVideo.getTitle());
                    videoVoList.add(videoVo);
                }
            }
            // 该章节的小节
            chapterVo.setChildren(videoVoList);

            chapterVoList.add(chapterVo);
        }

        return chapterVoList;
    }

    /**
     * 删除章节：如果该章节下有小节不能完成删除
     *
     * @param chapterId 章节id
     * @return 返回值
     */
    @Override
    public boolean deteleChapter(String chapterId) {
        // 查询该章节下是否有小节
        int count = eduVideoService.count(new LambdaQueryWrapper<EduVideo>()
                .eq(EduVideo::getChapterId, chapterId));

        if (count > 0) {
            throw new GuliException(20001, "该章节存在小节不能删除");
        } else {
            return this.removeById(chapterId);
        }
    }

    /**
     * 根据课程id删除章节
     *
     * @param courseId 课程id
     */
    @Override
    public void removeChapterByCourseId(String courseId) {
        this.remove(new LambdaQueryWrapper<EduChapter>().eq(EduChapter::getCourseId, courseId));
    }
}
